/**
 * 菜单管理
 * @constructor
 */
function Menu() {
}

Menu.init = function () {
    let vm = new Vue({
        el: "#customModule",
        data: {
            menu: "",
            choiceMenuName: "",//选中的menu名称
            rightSource: "",//右侧数据源
            roleName: "",//角色名称
            funList: "",//功能权限
            menuNo: "",//菜单编号
            menuName: "",//菜单名称
            menuUrl: "",//菜单路由
            menuLevel: "",//菜单级别
            activity: "",//菜单是否启用
            orderID: "",
            imageUrl: "",
            keyName: "",
            parentID: "",
            roleList: "",
            roleChecked: "1",
            roleID: ""
        },

        created() {
            this.roleName = $.cookie('RoleName');
            this.roleID = $.cookie('RoleIds');
        },
        mounted() {
            this.initMenuManage();
            this.initRole();
            setTimeout(() => {
                this.SpecialEffects();
            }, 10);

        },
        computed: {},
        watch: {},
        methods: {
            /**
             * 下拉框change事件
             */
            selectChanged() {
                this.initMenuManage();
            },
            /**
             * 获取当前角色下，可供选择的角色
             */
            initRole() {
                let url = "/RoleManage/initRole?roleID=" + $.cookie('RoleIds');
                axios.get(url).then(data => {
                    if (data.data.code === "200") {
                        this.roleList = [];
                        for (let i = 0; i < data.data.data.length; i++) {
                            let result = data.data.data[i];
                            let param = {
                                id: result.roleID,
                                name: result.roleName
                            };
                            this.roleList.push(param);
                        }
                    }
                }).catch(e => {

                })
            },
            initMenuManage() {
                let url = "/menuManage/menu";
                let pm = {"roleIds": this.roleChecked};
                axios.post(url, pm).then(data => {
                    if (data.data.code === "200") {
                        let menuList = data.data.data;
                        let result = [];
                        if (menuList.length > 0) {

                            //根据level进行分组
                            let groupResult = Help.groupBy(menuList, function (item) {
                                return [item.level];
                            });
                            //筛选结果为0到第一级
                            // let level0s = groupResult[0].filter(function(f){
                            //     if(f.parentID===0) return f;
                            // });
                            let level0s = groupResult[0].sort(function (a, b) {
                                return a.orderID - b.orderID;
                            });
                            //筛选其他级别菜单
                            let levelOther = Help.groupBy(groupResult[1], function (item) {
                                return [item.parentID];
                            });
                            for (let i = 0; i < level0s.length; i++) {
                                let paramParent = {
                                    textCH: "",//菜单名称
                                    id: "", //当前id
                                    parentID: "",//父ID
                                    level: "", //级别
                                    url: "",
                                    image: "",
                                    orderID: "",
                                    filterNo: "",
                                    children: []
                                };
                                //添加第一级别菜单
                                paramParent.textCH = level0s[i].textCH;
                                paramParent.id = level0s[i].id;
                                paramParent.parentID = level0s[i].parentID;
                                paramParent.orderID = level0s[i].orderID;
                                paramParent.Url = level0s[i].url;
                                paramParent.image = level0s[i].image;
                                paramParent.level = level0s[i].level;
                                paramParent.filterNo = level0s[i].id + '_' + level0s[i].parentID;

                                for (let k = 0; k < levelOther.length; k++) {
                                    for (let j = 0; j < levelOther[k].length; j++) {
                                        let sortResult = levelOther[k].sort(function (a, b) {
                                            return a.orderID - b.orderID;
                                        });
                                        if (sortResult[j].parentID === level0s[i].id) {
                                            let param = {
                                                textCH: "",//菜单名称
                                                id: "", //当前id
                                                parentID: "",//父ID
                                                level: "", //级别
                                                url: "",
                                                image: "",
                                                orderID: "",
                                            };
                                            //添加第二级别菜单
                                            param.textCH = sortResult[j].textCH;
                                            param.id = sortResult[j].id;
                                            param.parentID = sortResult[j].parentID;
                                            param.orderID = sortResult[j].orderID;
                                            param.Url = sortResult[j].url;
                                            param.image = sortResult[j].image;
                                            param.level = sortResult[j].level;
                                            paramParent.children.push(param);
                                        }
                                    }
                                }
                                result.push(paramParent);
                            }
                        }

                        vm.menu = result;
                        Help.alertSuccMessage("数据加载完毕");
                    } else {
                        vm.menu = "";
                    }
                }).catch(e => {
                    console.log(e);
                })
            },
            /**
             * 菜单特效处理函数
             * 及其他功能效果
             * @constructor
             */
            SpecialEffects() {
                var updateOutput = function (e) {
                    var list = e.length ? e : $(e.target),
                        output = list.data('output');
                    if (window.JSON) {
                        output.val(window.JSON.stringify(list.nestable('serialize')));//, null, 2));
                    } else {
                        output.val('JSON browser support required for this demo.');
                    }
                };
                // activate Nestable for list 1
                $('#nestable2').nestable({
                    group: 0,  //允许在列表之间拖动的组ID
                    maxDepth: 2 //树节点层次（默认5）
                }).on('change', updateOutput);

                // output initial serialised data
                updateOutput($('#nestable2').data('output', $('#nestable-output')));

                $('#nestable-menu').on('click', function (e) {
                    var target = $(e.target),
                        action = target.data('action');
                    if (action === 'expand-all') {
                        $('.dd').nestable('expandAll');
                    }
                    if (action === 'collapse-all') {
                        $('.dd').nestable('collapseAll');
                    }
                });
            },
            /**
             * 菜单点击事件
             * @param menuItem
             */
            menuClick(menuItem) {
                vm.choiceMenuName = menuItem.textCH;
                let url = "/menuManage/list";
                let pm = {"roleIds": this.roleChecked, "id": menuItem.id, "level": menuItem.level};
                axios.post(url, pm).then(data => {
                    let result = data.data.data;
                    let roleButton = result.RoleButton;
                    let roleMenu = result.RoleMenu;
                    let filter = [];
                    if (roleButton != null) {
                        for (let i = 0; i < roleButton.length; i++) {
                            let param = {
                                buttonID: "",
                                buttonDescription: "",
                                activity: "",
                                enDesc: ""
                            };
                            param.buttonID = roleButton[i].buttonID;
                            param.buttonDescription = roleButton[i].buttonDescription;
                            param.activity = roleButton[i].activity;
                            param.enDesc = roleButton[i].enDesc;
                            filter.push(param);
                        }
                    }

                    vm.funList = filter;
                    if (roleMenu.length > 0) {
                        vm.menuNo = roleMenu[0].id;
                        vm.menuName = roleMenu[0].textCH;
                        vm.menuUrl = roleMenu[0].url;
                        vm.menuLevel = roleMenu[0].level;
                        vm.activity = roleMenu[0].activity;
                        vm.orderID = roleMenu[0].orderID;
                        vm.imageUrl = roleMenu[0].imageUrl;
                        vm.keyName = roleMenu[0].keyName;
                        vm.parentID = roleMenu[0].parentID;
                    }
                    Help.alertSuccMessage('succ', '加载完毕')
                }).catch(e => {
                    console.log(e);
                }).finally(() => {
                    vm.isShow = true;
                })
            },
            /**
             * 左侧修改菜单提交
             */
            submitMenu() {
                let l = $('#submitMenu').ladda();
                l.ladda('start');
                try {
                    let val = $('#nestable-output').val();
                    if (val === "[]") {
                        l.ladda('stop');
                        return;
                    }
                    let url = "/menuManage/menuLeftEdit";
                    let pm = {source: val};
                    axios.post(url, pm).then(data => {
                        if (data.data.code === "200") {
                            vm.initMenuManage();
                            Help.alertSuccMessage('succ', '成功')
                        }
                    }).catch(e => {
                        console.log(e);
                    }).finally(() => {
                        l.ladda('stop');

                    });
                } catch (e) {
                    l.ladda('stop');
                    console.log(e);
                }

            },
            /**
             * 右侧修改菜单提交
             */
            rightSubmit() {
                let l = $('#submitMenu1').ladda();
                l.ladda('start');
                try {
                    let menuNo = vm.menuNo;
                    if (menuNo === "") {
                        l.ladda('stop');
                        Help.alertInfoMessage('提示', '没有发现修改的痕迹...');
                        return;
                    }
                    let funList = vm.funList;
                    let param = {
                        activity: vm.activity === true ? 1 : 0,
                        roleID: vm.roleChecked,
                        menu: {
                            textCH: vm.menuName,
                            url: vm.menuUrl,
                            id: vm.menuNo,
                            level: vm.menuLevel,
                            roleID: vm.roleChecked,
                            orderID: vm.orderID,
                            imageUrl: vm.imageUrl,
                            keyName: vm.keyName,
                            parentID: vm.parentID,
                        },
                        buttons: []
                    };
                    for (let i = 0; i < funList.length; i++) {
                        let childParam = {
                            buttonDescription: funList[i].buttonDescription,
                            activity: funList[i].activity,
                            enDesc: funList[i].enDesc,
                            id: funList[i].buttonID
                        };
                        param.buttons.push(childParam);

                    }
                    let url = "/menuManage/menuRightSubmit";
                    let pm = param;
                    axios.post(url, pm).then(data => {
                        if (data.data.code === "200") {
                            this.initMenuManage();

                        }

                    }).catch(e => {
                        console.log(e);
                    }).finally(() => {
                        l.ladda('stop');

                    });
                } catch (e) {
                    l.ladda('stop');
                    console.log(e);
                }
            },
            /**
             * 左侧取消菜单提交
             */
            submitCancel() {
                let l = $('#cancelMenu').ladda();
                l.ladda('start');
                $('#nestable-output').val('');
                vm.initMenuManage();
                l.ladda('stop');
                Help.alertInfoMessage('succ', '取消成功')
            },
            /**
             * 右侧取消菜单提交
             */
            submitRightCancel() {
                vm.menuNo = "";
                vm.menuName = "";
                vm.menuUrl = "";
                vm.menuLevel = "";
                vm.funList = "";
                Help.alertInfoMessage('succ', '取消成功')
            }
        }
    });
};
